﻿namespace Leetcode.N0517;
public class Solution
{
    public int FindMinMoves(int[] machines)
    {
        var davg = machines.Average();
        if (davg % 1 != 0)
        {
            return -1;
        }

        var step = 0;
        var val = 0;
        var avg = (int)davg;

        for (int i = 0; i < machines.Length; i++)
        {
            var curVal = machines[i] + val - avg;
            if (val < 0 && curVal > 0)
            {
                step = Math.Max(step, -val + curVal);
            }
            else
            {
                step = Math.Max(step, Math.Abs(curVal));

            }
            val = curVal;
        }
        return step;
    }
}